package zhonghuasuan;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class Zhonghuasuan {

	protected Shell shell;
	private Table table;
	private TableColumn tblclmnNewColumn;
	private TableColumn tableColumn;
	private TableItem tableItem;
	private Button btnNewButton;
	private TableItem tableItem_2;
	private TableItem tableItem_3;
	private TableCursor tableCursor;
	private TableItem tableItem_4;
	private TableColumn tableColumn_1;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Zhonghuasuan window = new Zhonghuasuan();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(682, 457);
		shell.setText("SWT Application");

		btnNewButton = SearchHandler.createSearchButton(shell);
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				 new Thread(new Runnable() {  
			            final Display display = Display.getDefault();  
			              
			            @Override  
			            public void run() {  
			                    
			                        	
			                        	CloseableHttpClient httpclient = HttpClients.createDefault();
			            				for(int i=0;i<100;i++){
			            					try {  
						                         Thread.sleep(100);  
						                     } catch (InterruptedException e) {  
						                         e.printStackTrace();  
						                     }
			            					String id = String.valueOf(69465+i);
			            					HttpGet httpget = new HttpGet("http://detail.zhonghuasuan.com/data/"+id);
			            					CloseableHttpResponse response;
			            					try {
			            						response = httpclient.execute(httpget);
			            						try {
			            							final String entity = EntityUtils.toString(response.getEntity());
//			            							System.out.println(entity);
			            							final Matcher m = Pattern.compile("\"discount\":").matcher(entity);
			            							boolean result = m.find();
			            							final Matcher m1 = Pattern.compile(",\"type\"").matcher(entity);
			            							boolean result1 = m1.find();
			            							if(result&&result1){
			            								System.out.println(entity.substring(m.end()+1, m1.start()-1));
			            							}
			            							
			            							final Matcher m2 = Pattern.compile("\"cost_price\":").matcher(entity);
			            							boolean result2 = m2.find();
			            							final Matcher m3 = Pattern.compile(",\"price\"").matcher(entity);
			            							boolean result3 = m3.find();
			            							final String id1 = id;
			            							display.asyncExec(new Runnable() {
			            								public void run() {
			            									TableItem tableItem_1 = new TableItem(table, SWT.NONE);
			            									tableItem_1.setText(new String[]{id1,entity.substring(m.end()+1, m1.start()-1),entity.substring(m2.end()+1, m3.start()-1)});
			            								}
			            							});
			            							
			            						} finally {
			            							response.close();
			            						}
			            					} catch (IOException e1) {
			            						e1.printStackTrace();
			            					}
			            					
			            				}
			            }  
			        }).start();  
				
//				ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10); 
//				fixedThreadPool.execute(new Runnable() {
//					
//					@Override
//					public void run() {
//						
//					}
//				});
			}
		});
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});

		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(10, 31, 656, 387);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(100);
		tblclmnNewColumn.setText("\u52171");

		tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(100);
		tableColumn.setText("\u52172");
		
		tableColumn_1 = new TableColumn(table, SWT.NONE);
		tableColumn_1.setWidth(100);
		tableColumn_1.setText("\u52173");
		
		
		

	}

	protected Button getBtnNewButton() {
		return btnNewButton;
	}
}
